/**    
 * @{#} PersonalActivity.java Create on 2014-5-29 下午9:26:48    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.Date;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.ui.ODatePicker;
import cn.o.app.ui.ODatePicker.OnPickDateListener;
import cn.o.app.util.ODateFormat;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class CreateBingliActivity extends BasicActivity {

	private TextView text_patient_name_value, text_sex_value,
			text_birthday_value, text_minzu_value, text_address_value,
			text_job_value, text_marry_value, text_huaiyun_value,
			text_bingshi_value, text_yaowuguomin_value,
			text_putongguominyuan_value, text_jiatingpifubing_value;
	
	private RelativeLayout rl_huaiyun;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_bingli);

		GlobalSettings.sBingLi = null;

		// 不为空则为编辑病历
		String bingli = getIntent().getStringExtra(Constant.IDENTITY_KEY);
		if (!CommonUtility.isNull(bingli)) {
			try {
				GlobalSettings.sBingLi = JsonUtil.convert(bingli, User.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		init();
		setNavTitle("创建病历");

		text_patient_name_value = (TextView) findViewById(R.id.text_patient_name_value);
		text_sex_value = (TextView) findViewById(R.id.text_sex_value);
		text_birthday_value = (TextView) findViewById(R.id.text_birthday_value);
		text_minzu_value = (TextView) findViewById(R.id.text_minzu_value);
		text_address_value = (TextView) findViewById(R.id.text_address_value);
		text_job_value = (TextView) findViewById(R.id.text_job_value);
		text_marry_value = (TextView) findViewById(R.id.text_marry_value);
		text_huaiyun_value = (TextView) findViewById(R.id.text_huaiyun_value);
		text_bingshi_value = (TextView) findViewById(R.id.text_bingshi_value);
		text_yaowuguomin_value = (TextView) findViewById(R.id.text_yaowuguomin_value);
		text_putongguominyuan_value = (TextView) findViewById(R.id.text_putongguominyuan_value);
		text_jiatingpifubing_value = (TextView) findViewById(R.id.text_jiatingpifubing_value);
		rl_huaiyun = (RelativeLayout) findViewById(R.id.rl_huaiyun);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
//		MobclickAgent.onResume(this);
		
		try {
			if (!CommonUtility.isNull(GlobalSettings.sBingLi)) {
				text_patient_name_value.setText(GlobalSettings.sBingLi.Name);
				int sex = GlobalSettings.sBingLi.Sex;
				text_sex_value.setText(CommonUtility.sex.get(sex));
				if (sex == 0) {
					rl_huaiyun.setVisibility(View.GONE);
				} else {
					// rl_huaiyun.setVisibility(View.VISIBLE);
					text_huaiyun_value.setText(CommonUtility.huaiyun
							.get(GlobalSettings.sBingLi.Pregnant_Status));
				}
				text_birthday_value.setText(CommonUtility.formatDate(
						GlobalSettings.sBingLi.Birth.toString(), 0, 10));
				text_minzu_value.setText(GlobalSettings.sBingLi.Nation);
				StringBuilder address = new StringBuilder();
				address.append(GlobalSettings.sBingLi.Province)
						.append(GlobalSettings.sBingLi.City)
						.append(GlobalSettings.sBingLi.County);
				text_address_value.setText(address);
				text_job_value.setText(GlobalSettings.sBingLi.Job);
				text_marry_value.setText(CommonUtility.marry
						.get(GlobalSettings.sBingLi.Marital_Status));
				text_bingshi_value
						.setText(CommonUtility.convertJSONArray2String(
								new JSONArray(
										JsonUtil.convert(GlobalSettings.sBingLi.MedicalHistory)),
								","));
				text_yaowuguomin_value
						.setText(CommonUtility.convertJSONArray2String(
								new JSONArray(
										JsonUtil.convert(GlobalSettings.sBingLi.DrugAllergen)),
								","));
				text_putongguominyuan_value
						.setText(CommonUtility.convertJSONArray2String(
								new JSONArray(
										JsonUtil.convert(GlobalSettings.sBingLi.Allergen)),
								","));
				text_jiatingpifubing_value
						.setText(CommonUtility.convertJSONArray2String(
								new JSONArray(
										JsonUtil.convert(GlobalSettings.sBingLi.FamilyMedicalHistory)),
								","));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onClick(View view) {
		Intent intent = null;
		int id = view.getId();
		if (id == R.id.text_patient_name) {
			intent = new Intent(activity, UpdateInfoInputActivity.class);
			intent.putExtra(Constant.IDENTITY_KEY_PARAM_KEY, "Name");
			intent.putExtra(Constant.IDENTITY_KEY_NAV_TITLE, "更改姓名");
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_sex) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { "[\"男\",\"女\"]", "修改性别", "Sex",
					CommonDataChooseActivity.TYPE_FIXATION,
					CommonDataChooseActivity.SINGLECHOOSE,
					CommonDataChooseActivity.DATA_TYPE_SEX };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_2, text_sex_value.getText().toString());
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_birthday) {
			ODatePicker datePicker = new ODatePicker(getContext());
			datePicker.show();
			datePicker.setListener(new OnPickDateListener() {

				@Override
				public void onPicked(ODatePicker picker, Date d, String time) {
					String date = ODateFormat.format(d, "yyyy-MM-dd");
					text_birthday_value.setText(date);
					if (!CommonUtility.isNull(date)) {
						HashMap<String, Object> params = new HashMap<String, Object>();
						params.put("Birth", date);
						params.put(Constant.PARAM_API, "MR.User.Set");
						params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
						if (!CommonUtility.isNull(GlobalSettings.sBingLi)) {
							try {
								params.put("MRUID", GlobalSettings.sBingLi.Uid);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						RequestTaskIntercept.requestIntercept(activity,
								Constant.URL.REQUEST, new RequestHandler(
										activity, new HandlerCallback() {

											@Override
											public void callback(Object o) {
												if (!CommonUtility.isNull(o)) {
													JSONObject object = (JSONObject) o;
													if (CommonUtility
															.response200(object)) {
														CommonUtility.toast(
																activity,
																"保存成功");
														try {
															GlobalSettings.sBingLi = JsonUtil
																	.convertFromObject(
																			object.getJSONObject("Res"),
																			User.class);
														} catch (Exception e) {
															e.printStackTrace();
														}
													} else {
														CommonUtility.toast(
																activity,
																"保存失败");
													}
												}
											}
										}), params);
					}
				}
			});
		} else if (id == R.id.text_minzu) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { Constant.URL.REQUEST_NATION_JSON, "修改民族",
					"Nation", CommonDataChooseActivity.TYPE_URL,
					CommonDataChooseActivity.SINGLECHOOSE, "" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_address) {
			intent = new Intent(activity, MulitLevelDataChooseActivity.class);
			String[] datas = {
					Constant.URL.REQUEST_AREA_JSON,
					"选择省份",
					"Province" + Constant.SPLIT + "City" + Constant.SPLIT
							+ "County" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_job) {
			intent = new Intent(activity, MulitLevelDataChooseActivity.class);
			String[] datas = { Constant.URL.REQUEST_JOBS_JSON, "选择行业", "Job" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_LEVEL_2,
					Constant.IDENTITY_KEY_LEVEL_2);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_marry) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { "[\"未婚\",\"已婚\"]", "修改婚姻状况", "Marital_Status",
					CommonDataChooseActivity.TYPE_FIXATION,
					CommonDataChooseActivity.SINGLECHOOSE,
					CommonDataChooseActivity.DATA_TYPE_MARRY };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_huaiyun) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { "[\"未孕\",\"怀孕\"]", "修改怀孕状况", "Pregnant_Status",
					CommonDataChooseActivity.TYPE_FIXATION,
					CommonDataChooseActivity.SINGLECHOOSE,
					CommonDataChooseActivity.DATA_TYPE_HUAIYUN };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_bingshi) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { Constant.URL.REQUEST_MEDICALHISTORY_JSON,
					"修改病史", "MedicalHistory",
					CommonDataChooseActivity.TYPE_URL,
					CommonDataChooseActivity.MULITCHOOSE, "" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_yaowuguomin) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { Constant.URL.REQUEST_DRUGALLERGEN_JSON,
					"修改药物过敏", "DrugAllergen",
					CommonDataChooseActivity.TYPE_URL,
					CommonDataChooseActivity.MULITCHOOSE, "" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_putongguominyuan) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { Constant.URL.REQUEST_ALLERGEN_JSON, "修改普通过敏源",
					"Allergen", CommonDataChooseActivity.TYPE_URL,
					CommonDataChooseActivity.MULITCHOOSE, "" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.text_jiarenpifubing) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { Constant.URL.REQUEST_DISEASE_JSON, "修改家人皮肤病情况",
					"FamilyMedicalHistory", CommonDataChooseActivity.TYPE_URL,
					CommonDataChooseActivity.MULITCHOOSE, "" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_BINGLI, true);
			startActivity(intent);
		} else if (id == R.id.btn_left) {
			if (!CommonUtility.isNull(GlobalSettings.sBingLi)) {
				setResult(20);
			}
			finish();
		}
	}
	

/*	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/

}
